log using elec-log.log, replace name(Hearn_2023)


*  ***************************************************************** *
*  ***************************************************************** *
*     File-Name:   replication.do                                    *
*     Author:      Eddie Hearn                                       *
*     Purpose:     Hearn 2023 (MJLAS)                                *
*     Input File:  elec-raw.csv                                      *
*     Program:	   Stata 17                                          *
*     OS:		   Debian GNU/Linux                                  *
*  ****************************************************************  *
*  ****************************************************************  *

** The 2020 Presidential Election and Ally Perceptions of the US: A Natural Experiment in Japan

    set more off 
    set scheme s1color
**************************
*load additional packages
**************************

    capture ssc install outreg2 

**************************
*make directories
**************************

    capture mkdir stata-replication  
    capture mkdir stata-replication/tables 
    capture mkdir stata-replication/figures 
    capture mkdir stata-replication/data

****************************
*load data -set delimiter
****************************

   import delimited data/election-raw.csv, varname(1)   clear
   #delimit ;
   
***********************************;
*controls: demographic variables   ;
***********************************;

    gen age = q1-1 /*set min to 1*/ ;
    gen region = q2 ;
    gen female = q3 -1;
    gen educ = q8;
    gen collgrad = 1 if educ>8;
        replace collgrad = 0 if educ <=8;
        replace collgrad =. if educ==.;
    gen inc = q9;
    gen ldp_s = 3 if q7==1;
        replace ldp_s =2 if q7==3;
        replace ldp_s =1 if q7==2;
    drop q1-q3 q7-q9;

*********************************;
*compare to national averages    ;
*********************************;

    tab region;
    tab age;
    tab female;
    tab collgrad;
    tab inc;

****************************;
*iv: treatment variable     ;
****************************;

    gen elec_tr = 1 
        if respid >1000 /*after nov 12*/;
    replace elec_tr = 0 
        if respid <1000 /*before oct 22*/;
    drop respid ;

***************************;
*interaction variable      ;
***************************;

    gen ldp_x_elec = ldp_s*elec_tr;

****************************;
*dv: attitude toward us     ;
****************************;
    
    local dv us_depend us_trust us_infl ;
    local i = 4 ;
    foreach x in `dv' { ;
        gen `x' = (q`i'*-1) + 6 /*invert and set min to 1*/;
        drop q`i' ;
        local i = `i' + 1;
    };

    gen index = (us_depend + us_trust + us_infl)-2; 
    /*additive index - set min to 1*/;

****************************;
*make figures in python     ;
****************************;

/*python3 must be available to create graphs. If python3 is not available the rest of the do-file will compile*/;
    outsheet * using stata-replication/data/graphs.csv , comma replace ;
    capture shell python3 graphs.py ;
    capture rm graphs.txt ;
    if _rc!=0 {;
        display "Python failed...generating generic Stata graphs instead" ;
        foreach x in `dv' { ;
            graph bar, over(`x') by(elec_tr) ;
            graph export stata-replication/figures/`x'2.svg, replace;
            local ldp = 1 ;
            while `ldp' <4  {;
                graph bar if  ldp_s ==`ldp', over(`x') by(elec_tr) ;
                graph export stata-replication/figures/`x'_`ldp'.svg, replace;
                local ldp = `ldp' +1 ;
             } ;
        } ;
    } ;
    else {;
        display "Graphs generated by python" ;
        } ;

*************************;
*Add labels              ;
*************************;

    label variable region "Geographic region"   ;
    label variable age "Age cohort"   ;
    label variable female "Gender"   ;
    label variable educ "Education"   ;
    label variable collgrad "College Graduate" ;
    label variable inc "Family income"   ;
    label variable ldp_s "Support for LDP"   ;
    
    label define area 1 "Hokaido" 2 "Tohoku" 3 "Kanto" 4 "Chubu" 5 "Kinki" 6 "Chugoku" 7 "Shikoku" 8 "Kyushu"; 
    label values region area;
    tabulate region, generate(region);
    
    label define cohort 1 "18-29" 2"30-39" 3 "40-49" 4 "50-59" 5 "60-99"   ;
    label values age cohort   ;

    label define gender 0 "male" 1 "female"   ;
    label values female gender   ;
    
    label define edyrs 1 "Junior high" 2 "Some high school" 3 "High school grad" 4 "Some trade school" 5 "Trade school grad" 6 "Some juior college" 
        7 "Junior college grad"  8 "Some university" 9 "University grad" 10 "Some graduate school" 11 "Graduate degree"   ; 
    label values educ edyrs   ;

    label define finc 1 "Less than 3 million" 2 "3-4 million" 3 "4-5 million" 4 "5-6 million" 5 "6-7 million" 6 "7-8 million" 7 "8-9 million"  8 "9-10 million" 9 "10-12 million" 10 "more than 12 million"   ;
    label values inc finc   ;

    label define exp_group 0 "Control group" 1 "Treatment group"; 
    label values elec_tr exp_group;
    label variable elec_tr "Treatment"   ;

    label variable ldp_x_elec   "Interaction Term" ;

    label define likert 1 "strongly disagree" 2 "disagree" 3 "neither" 4 "agree" 5 "strongly agree";
    label values us_* likert;
    
    label variable us_depend "Japan can rely on the US as a dependable ally";
    label variable us_trust "I am confident that US leaders will make good decisions";
    label variable us_inf "It is good if American ideas and customs spread to Japan";
    
*************************;
*election models         ;
*************************;

    local label "label" ;
    local type "tex replace" ;
    local m=1 ;
    local title "title(Effect of 2020 Election)" ;
    foreach x in `dv' { ;
        oprobit `x' elec_tr ;
            outreg2 using stata-replication/tables/main,
                `type'
                `label'
                `title'
                ctitle(Model `m', `x') ;
            local type "tex append" ;
            local title "" ;
            local m = `m'+1 ;
        oprobit `x' elec_tr ldp_s ldp_x_elec ;
            outreg2 using stata-replication/tables/main, 
                `type'
                ctitle(Model `m', `x');
            local m = `m'+1 ;
        oprobit `x' elec_tr ldp_s ldp_x_elec age female collgrad inc region2 region3 region4 region5 region6 region7 region8;
            outreg2 using stata-replication/tables/main, 
                `type'
                ctitle(Model `m', `x');
            local m = `m'+1 ;
    };

*******************************************************************************;
*marginal effects - monte carlo simulation to calculate confidence intervals   ;
*******************************************************************************;  
    
    foreach x in `dv' { ;
        local b: variable label `x';
        oprobit `x' elec_tr ldp_s ldp_x_elec age female collgrad inc region2 region3 region4 region5 region6 region7 region8;
        preserve;
        set seed 1768534  /*Musashi University zipcode*/;
        drawnorm MG_b1-MG_b18, 
            n(1000) means(e(b)) cov(e(V))clear;
        summarize;
        gen ldp =. ;
        local i = 1 ;
        while `i' <4 { ;
            replace ldp = `i' if _n==`i';
            local i = `i'+1 ;
        } ;         
        gen higher =. ;
        gen mean =. ;
        gen lower =. ;
        local j =1 ;
        while `j' <4 { ;
            gen mef`j' = MG_b1 + (`j'*MG_b3); 
            centile mef`j', centile(2.5, 50, 97.5) ;
            replace higher =`r(c_3)'  if _n==`j'  ;
            replace mean =`r(c_2)'     if _n==`j' ;
            replace lower =`r(c_1)'    if _n==`j';
            local j = `j'+1 ;
        } ; 
        twoway rcap higher lower ldp|| scatter mean ldp,
            xlabel(1 "LDP-K Opposition" 2 "Nuetral" 3 "LDP-K Support" )
            xsc(r(.5 3.5))
            xtitle("")
            ytitle("Marginal Effect")
            legend(order(1 "95% confidence interval"))
            title("`b'", width(100))
            yline(0, lstyle(foreground));
        graph export stata-replication/figures/mef_`x'.svg, replace;
 
        *******************************************************************************;
        *predicted probabilities                                                       ;
        *******************************************************************************; 

        scalar h4 = 5;
        scalar h7 = 3;
        scalar h9 = 1;
        local ldp = 1;
        while `ldp' <4  {;
            local trt = 0 ;
            while `trt' <2 {;
                local e = 1 ;
                local t = 15 ;
                while `e' < 5 {;
                    gen `x'_`ldp'_`trt'_eta`e' = MG_b`t'-(MG_b1*`trt'+MG_b2*`ldp' + MG_b3*`trt'*`ldp' + MG_b4*h4 + MG_b5 + MG_b7*h7 + MG_b9);
                    local e = `e'+1 ;
                    local t = `t'+1 ;
                } ;
                local n = 1 ;
                while `n' < 5 {;
                    gen `x'_`ldp'_`trt'_norm`n' = normal(`x'_`ldp'_`trt'_eta`n');
                    local n = `n'+1 ;
                } ;
                gen `x'_`ldp'_`trt'_p1 = `x'_`ldp'_`trt'_norm1;
                gen `x'_`ldp'_`trt'_p2 = `x'_`ldp'_`trt'_norm2-`x'_`ldp'_`trt'_norm1;
                gen `x'_`ldp'_`trt'_p3 = `x'_`ldp'_`trt'_norm3-`x'_`ldp'_`trt'_norm2;
                gen `x'_`ldp'_`trt'_p4 = `x'_`ldp'_`trt'_norm4-`x'_`ldp'_`trt'_norm3;
                gen `x'_`ldp'_`trt'_p5 = 1-`x'_`ldp'_`trt'_norm4;
                local trt = `trt'+1;
            };
            local d = 1 ;
            while `d' < 6  {;
                gen `x'_`ldp'_diff`d' = `x'_`ldp'_1_p`d' - `x'_`ldp'_0_p`d';
            local d = `d'+1 ;
            } ;
            local ldp = `ldp'+1;
        } ;
        drop M* ;
        sum *_p* ;
        sum *diff* ;
        centile *diff*, centile(2.5 97.5);
        save stata-replication/data/sim_`x'.dta, replace ;
        restore ;
    };  
       
*********************************;
*descriptive stats               ;
*********************************;

    outreg2 using stata-replication/tables/decrip.tex, 
        replace 
        sum(log) 
        wide;

***************************;
*balance check             ;
***************************;

    probit elec_tr  ldp_s age  female collgrad inc region2 region3 region4 region5 region6 region7 region8 ;
    outreg2 using stata-replication/tables/balance, 
        tex replace 
        ctitle(Model 1) 
        title(Randomization-Balance Check);

************************;
*    index              ;
************************;

    reg index elec_tr;
    outreg2 using stata-replication/tables/index, 
        tex replace 
        ctitle(Model 1, Basic) 
        title(Index);

    reg index elec_tr ldp_s ldp_x_elec;
    outreg2 using stata-replication/tables/index, 
        tex append 
        ctitle(Model 2, Partisan);

    reg index elec_tr ldp_s ldp_x_elec age female educ inc region2 region3 region4 region5 region6 region7 region8 ;
    outreg2 using stata-replication/tables/index, 
        tex append 
        ctitle(Model 3, Controls);

log close Hearn_2023;

************************;
*    clean up;
************************;

cp elec-log.log stata-replication/elec-log.log, replace;
rm elec-log.log;

